from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup
from aw.pom import Setting, Keyboard, DeskTop


class PreTest_RK3568_Basic_Setting_2000(TestCase, Setting):
    # 预置条件：
    # 1.进入桌面，点击设置-生物识别和密码-锁屏密码，点击〇框，输入6位数字
    # 2.再次输入相同的6位数字
    # 3.设备息屏后重新进入
    # 预期结果：
    # 1.正常唤醒键盘，可输入6位PIN数字密码，如果输入的不是数字，则有“PIN应为6位数字”的提示信息
    # 2.输入完成后会自动返回到生物识别和密码界面，显示有“关闭锁屏密码”的提示按钮
    # 3.需要输入锁屏密码才能进入，输入错误密码会提示“密码错误”

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：点击设置-生物识别和密码-锁屏密码，点击〇框，输入6位数字')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.touchByText(self.Phone1, '生物识别和密码')
        CommonOH.touchByText(self.Phone1, '锁屏密码')
        CommonOH.wait(self.Phone1, 0.5)
        CommonOH.click(self.Phone1, **self.pwd_input_area)
        CommonOH.wait(self.Phone1, 2)
        Step('预期结果1：可正常唤起键盘')
        CommonOH.checkIfTextExist(self.Phone1, 'space')
        CommonOH.touchByText(self.Phone1, 'space')
        CommonOH.wait(self.Phone1, 2)
        CommonOH.checkIfTextExist(self.Phone1, 'PIN 应为 6 位数字')
        # 删除字符，切换到数字键盘
        CommonOH.click(self.Phone1, **Keyboard.delete)
        try:
            CommonOH.touchByText(self.Phone1, '?123')
        except:
            pass
        # 输入密码123456
        CommonOH.touchByText(self.Phone1, '1')
        CommonOH.touchByText(self.Phone1, '2')
        CommonOH.touchByText(self.Phone1, '3')
        CommonOH.touchByText(self.Phone1, '4')
        CommonOH.touchByText(self.Phone1, '5')
        CommonOH.touchByText(self.Phone1, '6')
        CommonOH.wait(self.Phone1, 0.5)
        # 再次输入密码
        Step('步骤2：再次输入锁屏密码123456')
        CommonOH.touchByText(self.Phone1, '1')
        CommonOH.touchByText(self.Phone1, '2')
        CommonOH.touchByText(self.Phone1, '3')
        CommonOH.touchByText(self.Phone1, '4')
        CommonOH.touchByText(self.Phone1, '5')
        CommonOH.touchByText(self.Phone1, '6')
        CommonOH.wait(self.Phone1, 0.5)
        Step('预期结果2：自动返回至生物识别和密码界面')
        CommonOH.checkIfTextExist(self.Phone1, '更改锁屏密码')
        CommonOH.goHome(self.Phone1)
        Step('步骤3.设备息屏后重新进入,输入错误密码')
        CommonOH.pressPowerKey(self.Phone1)
        CommonOH.pressPowerKey(self.Phone1)
        CommonOH.swipe(self.Phone1, **DeskTop.swipe_up)
        CommonOH.wait(self.Phone1, 0.5)
        Step('预期结果3.需要输入锁屏密码才能进入，输入错误密码会提示“密码错误”')
        # CommonOH.checkIfTextExist(self.Phone1, '输入密码')
        CommonOH.touchByText(self.Phone1, '4')
        CommonOH.touchByText(self.Phone1, '5')
        CommonOH.touchByText(self.Phone1, '6')
        CommonOH.touchByText(self.Phone1, '1')
        CommonOH.touchByText(self.Phone1, '2')
        CommonOH.touchByText(self.Phone1, '3')
        CommonOH.wait(self.Phone1, 2)
        CommonOH.checkIfTypeExist(self.Phone1, 'Badge', expect=False)
        Step('步骤4.输入正确的锁屏密码')
        CommonOH.touchByText(self.Phone1, '1')
        CommonOH.touchByText(self.Phone1, '2')
        CommonOH.touchByText(self.Phone1, '3')
        CommonOH.touchByText(self.Phone1, '4')
        CommonOH.touchByText(self.Phone1, '5')
        CommonOH.touchByText(self.Phone1, '6')
        Step('预期结果4.进入桌面')
        CommonOH.checkIfTypeExist(self.Phone1, 'Badge')

    def teardown(self):
        Step('收尾1：强制删除/data/*目录并重启，为了清理设置的锁屏密码')
        CommonOH.shell(self.Phone1, 'rm -rf /data/*')
        CommonOH.safeReboot(self.Phone1)
